<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>对数组中的每个成员递归地应用用户函数</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array-walk-recursive" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_walk_recursive</h1>
  <p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_walk_recursive</span> &mdash; <span class="dc-title">对数组中的每个成员递归地应用用户函数</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-walk-recursive-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_walk_recursive</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$array</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$userdata</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span>
  ] ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   将用户自定义函数 <code class="parameter">callback</code> 应用到
   <code class="parameter">array</code> 数组中的每个单元。本函数会递归到更深层的数组中去。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-walk-recursive-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       输入的数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">callback</code></dt>

     <dd>

      <p class="para">
       典型情况下
   <code class="parameter">callback</code> 接受两个参数。<code class="parameter">array</code>
   参数的值作为第一个，键名作为第二个。
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        如果 <code class="parameter">callback</code> 需要直接作用于数组中的值，则给
         <code class="parameter">callback</code> 的第一个参数指定为<a href="language.references.html" class="link">引用</a>。这样任何对这些单元的改变也将会改变原始数组本身。
       </p>
      </p></blockquote>
     </dd>

    
    
     <dt>
<code class="parameter">userdata</code></dt>

     <dd>

      <p class="para">
       如果提供了可选参数
        <code class="parameter">userdata</code>，将被作为第三个参数传递给 <code class="parameter">callback</code>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>



 <div class="refsect1 returnvalues" id="refsect1-function.array-walk-recursive-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.array-walk-recursive-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6081">
    <p><strong>Example #1 <span class="function"><strong>array_walk_recursive()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$sweet&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'a'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$fruits&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'sweet'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$sweet</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'sour'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'lemon'</span><span style="color: #007700">);<br /><br />function&nbsp;</span><span style="color: #0000BB">test_print</span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$key</span><span style="color: #DD0000">&nbsp;holds&nbsp;</span><span style="color: #0000BB">$item</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">array_walk_recursive</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'test_print'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
a holds apple
b holds banana
sour holds lemon
</pre></div>
    </div>
    <div class="example-contents"><p>
     注意上例中的键 &#039;<em>sweet</em>&#039; 并没有显示出来。任何其值为 <span class="type"><a href="language.types.array.html" class="type array">array</a></span> 的键都不会被传递到回调函数中去。
    </p></div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-walk-recursive-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="array_walk.html" class="function" rel="rdfs-seeAlso">array_walk()</a> - 使用用户自定义函数对数组中的每个元素做回调处理</span></li>
    <li class="member"><a href="language.pseudo-types.html#language.types.callback" class="link">callback</a> 类型的信息</li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>